<?php

namespace app\modules\clinical\models;

use Yii;

/**
 * This is the model class for table "check".
 *
 * @property integer $pk_id
 * @property integer $pt_id
 * @property integer $rel_id
 * @property string $ck_type
 * @property string $ck_program
 * @property string $ck_result
 * @property string $ck_range
 * @property string $ck_unit
 * @property string $ck_date
 * @property string $pic_id
 * @property string $crt_name
 * @property string $crt_time
 */
class Check extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'check';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['pt_id', 'ck_type', 'ck_program', 'ck_date'], 'required'],
            [['pt_id', 'rel_id', 'ck_program'], 'integer'],
            [['ck_type'], 'string'],
            [['ck_date', 'crt_time', 'update_at'], 'safe'],
            [['ck_result', 'ck_range', 'ck_unit', 'pic_id', 'crt_name', 'update_name'], 'string', 'max' => 30]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'pk_id' => 'Pk ID',
            'pt_id' => 'Pt ID',
            'ck_type' => '类别',
            'ck_program' => '检查项目',
            'ck_result' => '检查结果',
            'ck_range' => '参考范围',
            'ck_unit' => '单位',
            'ck_date' => '报告日期',
            'crt_name' => '创建人',
            'crt_time' => '创建日期',
            'update_name' => '修改人',
            'update_at' => '修改日期',

            'ppt_id' => '病案号',
            'pt_name' => '患者姓名',
        ];
    }

    public function getPatient()
    {
        return $this->hasOne(Patient::className(), ['pk_id' => 'pt_id']);
    }

    public function defaultValue()
    {
        $this->ck_date = date("Y-m-d");
    }

    public function beforeSave($insert)
    {
        if(parent::beforeSave($insert)){
            if($this->isNewRecord){
                $this->crt_time = date('y-m-d H:i:s');
                $this->crt_name = Yii::$app->user->identity->username;
            }else{
                $this->update_at = date('y-m-d H:i:s');
                $this->update_name = Yii::$app->user->identity->username;
            }
            return true;
        }else{

            return false;
        }
    }

    public static function getParams($type = false, $code = false) {
        $params = [
            'check_type' => array('lab' => '实验室检查', 'media' => '影像学检查'),
            'ck_result' =>  array('正常', '异常'),
            'ck_program' => array('CT', 'MRI', 'fMRI', 'PED-DAT'),
        ];

        if ($code !== false) {
            return ($type) ? $params[$type][$code] : '';
        }

        return ($type) ? $params[$type] : $params;
    }
}
